﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFlickr.Communication;
using NFlickr.Factory;
using NFlickr.Core.Interface;
using System.Xml.Linq;

namespace NFlickr.Core
{
    public class PhotoSet : RequireSignedServiceBase
    {
        private const string CreatePhotoSet = "flickr.photosets.create";
        private const string AddPhotoToSet = "flickr.photosets.addPhoto";

        public PhotoSet(FlickrService service)
        {
            Frob = service.Frob;
            Token = service.Token;

        }

        public PhotoSet(FlickrService service, string title)
        {
            Frob = service.Frob;
            Token = service.Token;

        }

        private string _methodName = string.Empty;
        public override string MethodName
        {
            get
            {
                return _methodName;
            }
        }

        public override string TagName
        {
            get { return "photoset"; }
        }



        public Frob Frob
        {
            get;
            internal set;
        }

        public string Title
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public string ID
        {
            get;
            internal set;
        }

        public string Url
        {
            get;
            internal set;
        }

        public Token Token
        {
            get;
            internal set;
        }

        public IList<Photo> Photoes
        {
            get;
            set;
        }

        public Photo PrimaryPhoto
        {
            get;
            internal set;
        }

        public bool Create()
        {
            _methodName = CreatePhotoSet;
            IChannel channel = PrepareChannel();
            Params.Add("title", Title);

            if (!string.IsNullOrEmpty(Description))
            {
                Params.Add("description", Description);
            }

            Params.Add("primary_photo_id", PrimaryPhoto.PhotoId);


            IResult result = channel.SendRequest(FlickrServiceUrl, this);

            if (result != null && result.ResultCode == 0)
            {
                return true;
            }
            return false;
        }

        public bool Add(Photo photo)
        {
            _methodName = AddPhotoToSet;
            IChannel channel = PrepareChannel();

            Params.Add("photoset_id", this.ID);
            Params.Add("photo_id", photo.PhotoId);


            IResult result = channel.SendRequest(FlickrServiceUrl, this);

            if (result != null && result.ResultCode == 0)
            {
                return true;
            }
            return false;

        }

        private IChannel PrepareChannel()
        {
            IChannel channel = FlickrChannelFactory.CreateInstance();
            channel.Complete += new EventHandler<SendRequestCompletedEventArgs>(channel_Complete);
            channel.SignatureRequired += new NFlickr.Core.Interface.SignatureRequiredEventHandler(channel_SignatureRequired);


            Params = new Dictionary<string, string>();

            Params.Add(NFlickr.Resource.FlickrService.ApplicationKeyName, ApplicationKey);
            Params.Add(NFlickr.Resource.FlickrService.MethodKeyName, MethodName);
            Params.Add(NFlickr.Resource.FlickrService.FrobKeyName, Frob.Value);
            Params.Add("auth_token", Token.Value);

            return channel;
        }

        public override bool Refresh()
        {
            return false;
        }

        string channel_SignatureRequired(object sender, NFlickr.Core.Interface.SignatureRequiredEventArgs args)
        {
            Params.Add(NFlickr.Resource.FlickrService.SignatureKeyName, args.Singnature);
            return GetQueryString(Params);
        }

        void channel_Complete(object sender, NFlickr.Core.Interface.SendRequestCompletedEventArgs args)
        {

            args.Result = ParseResult(args.Response);
            if (args.Result.ResultCode == 0)
            {
                XElement document = XDocument.Parse(args.Response).Element("rsp");
                XElement setElement = document.Element(TagName);
                if (setElement != null)
                {
                    ID = setElement.Attribute("id").Value;
                    Url = setElement.Attribute("url").Value;
                }
            }
            _methodName = string.Empty;
        }
    }
}
